package com.utils.toast

import android.widget.Toast
import com.utils.LogUtils
import com.utils.Utils
import com.utils.toast.ext.IconToastView
import com.utils.toast.ext.TextToastView

/**
 * @describe: 系统toast, 用于app运行在后台时，
 * 提示：Android 11之后，app在后台不允许自定义Toast
 * @Author: lixiaopeng
 * @Date: 4/6/21
 */
class SystemToast : BaseToast() {
    private var toast: Toast? = null

    override fun show() {
        toast =  Toast.makeText(Utils.getApp(),getToastTxt(),this@SystemToast.toastDuration).apply {
            setGravity(gravity, xOffset, yOffset)
            show()
        }
    }
    private fun getToastTxt():String{
        return when (toastView) {
            is TextToastView -> {
                (toastView as TextToastView).text
            }
            is IconToastView -> {
                (toastView as IconToastView).text
            }
            else -> {
                LogUtils.w("WindowToast","不支持该类型")
                ""
            }
        }
    }

    override fun hide() {
        toast?.cancel()
    }
}
